package com.rongshu.view.login.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.login.contract.SetPenNameView;
import com.rongshu.view.login.entity.PerfectPenName;
import com.rongshu.view.login.presenter.SetPenNamePresenter;
import com.rongshu.widget.BanyanDialog;
import com.widget.CircleImageView;
import com.widget.EditTextWithDel;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * description：设置笔名
 */
public class SetPenNameActivity extends BaseActivity<SetPenNamePresenter> implements SetPenNameView {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.head_content)
    RelativeLayout headContent;
    @BindView(R.id.edit_content)
    EditTextWithDel editContent;
    @BindView(R.id.user_head)
    CircleImageView userHeadImg;

    //用户头像
    private String userHead;
    //用户笔名
    private String userName;
    //图片路径
    private String imgPath;

    /**
     * userName 用户名
     * userHead 用户头像，若无头像可传空
     */
    public static void toSetPenNameActivity(Context context, String userName, String userHead) {
        Intent intent = new Intent(context, SetPenNameActivity.class);
        intent.putExtra(Constant.INTENT_MESSAGE, userName);
        intent.putExtra(Constant.INTENT_DATA, userHead);
        context.startActivity(intent);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_set_pen_name;
    }

    @Override
    protected void initView() {
        //意图数据
        Intent intent = getIntent();
        if (intent != null) {
            userName = intent.getStringExtra(Constant.INTENT_MESSAGE);
            userHead = intent.getStringExtra(Constant.INTENT_DATA);
        }
        headMiddle.setText(getString(R.string.set_bi_name));
        if (!TextUtils.isEmpty(userHead)) {
            Glide.with(this).load(userHead).into(userHeadImg);
        }
        editContent.setText(userName);
        editContent.setSelection(userName.length());
        //set presenter
        setPresenter(new SetPenNamePresenter(this), this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.head_right, R.id.user_head, R.id.submit_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.head_right:
                showKeyboard(false);
                finish();
                break;
            case R.id.user_head:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .selectionMode(PictureConfig.SINGLE)
                        .previewImage(true)
                        .isCamera(true)
                        .enableCrop(true)
                        .withAspectRatio(1, 1)
                        .circleDimmedLayer(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.submit_btn:
                BanyanDialog.Builder builder = new BanyanDialog.Builder(this);
                builder.setTitle("笔名确认后将无法修改");
                builder.setLeft("取消");
                builder.setRight("确认");
                final BanyanDialog banyanDialog = builder.create();
                builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
                    @Override
                    public void leftClickListener() {
                        banyanDialog.dismiss();
                    }

                    @Override
                    public void rightClickListener() {
                        String bi_name = editContent.getText().toString().trim();
                        if (TextUtils.isEmpty(bi_name)) {
                            toast(getString(R.string.please_set_bi_name));
                            return;
                        }
                        showLoading(true);
                        if (TextUtils.isEmpty(imgPath)) {
                            uploadImgSuccess(userHead);
                        } else {
                            presenter.postUploadFileBase64(imgPath);
                        }
                        banyanDialog.dismiss();
                    }
                });
                banyanDialog.show();
                break;
        }
    }

    /**
     * 上传头像成功
     */
    @Override
    public void uploadImgSuccess(String url) {
        PerfectPenName perfectPenName = new PerfectPenName();
        perfectPenName.setBi_name(editContent.getText().toString().trim());
        perfectPenName.setAvatar(url);
        presenter.perfectPanName(perfectPenName);
    }

    /**
     * 设置笔名返回结果
     */
    @Override
    public void onSetPenName() {
        showLoading(false);
        showKeyboard(false);
        finish();
    }

    /**
     * 接口请求失败
     */
    @Override
    public void iNetFailure(String errorMsg) {
        toast(errorMsg);
        showLoading(false);
        showKeyboard(false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //相册返回
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                imgPath = selectList.get(0).getCutPath();
                Glide.with(this).load(imgPath).into(userHeadImg);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PictureFileUtils.deleteCacheDirFile(this);
    }
}
